<template>
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData, readonly, mode }"
    :api-map="singleProductContractCRUD"
    header="*合同"
    :on-before-save-data="handleDialogSaveData"
    :on-before-set-data="handleDialogSetData"
    
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item label="送货时间" name="deliverGoodsTime" :rules="[{ required: true, message: '请选择送货时间' }]">
      <t-date-picker 
        allow-input
        v-model:value="formData.deliverGoodsTime"
        clearable
        placeholder="请选择送货时间"
        value-type="time-stamp"
        :readonly="readonly"
        style="width: 100%"
        @change="
          (value) => {
            if (value) {
              value = dayjs(value).format('YYYY-MM-DD');
              value = dayjs(value).valueOf();
            }
            formData.deliverGoodsTime = value ? value : undefined;
          }
        "
      />
    </t-form-item>
    <t-form-item label="收货人" name="consignee" :rules="[{ required: true, message: '请输入收货人' }]">
      <t-input
        v-model:value="formData.consignee"
        placeholder="请输入收货人"
        :readonly="readonly"
        style="width: 100%"
      />
    </t-form-item>
    <t-form-item label="收货地址" name="deliverGoodsAddress" :rules="[{ required: true, message: '请输入收货地址' }]">
      <t-input
        v-model:value="formData.deliverGoodsAddress"
        placeholder="请输入收货地址"
        :readonly="readonly"
        style="width: 100%"
      />
    </t-form-item>
    <t-form-item label="合同总价" name="productContractPrice" :rules="[{ required: true, message: '请输入合同总价' }]">
      <t-input-number
        v-model:value="formData.productContractPrice"
        :decimal-places="2"
        min="0"
        :allow-input-over-limit="false"
        placeholder="请输入合同总价"
        :readonly="readonly"
        style="width: 100%"
      />
    </t-form-item>
    <t-form-item label="合同文件" name="fileUrl" :rules="[{ required: true, message: '请上传合同文件' }]">
      <upload v-model:files="formData.fileUrl" multiple :readonly="readonly" />
    </t-form-item>


    
  </custom-dialog>
</template>

<script setup lang="ts">
import dayjs from 'dayjs';
import { MessagePlugin } from 'tdesign-vue-next';
import { ref, useTemplateRef, watch } from 'vue';

import { singleProductContractCRUD } from '@/api/order/single'
import CustomDialog from '@/components/custom/dialog/index.vue';
import ImageViewer from '@/components/image-viewer/index.vue';
import Upload from '@/components/upload/index.vue';
import { useDictionaryStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// props
const props = defineProps({
  // 公司id
  companyId: {
    type: Number,
  },
});



// emits
const emits = defineEmits(['requestSuccess']);
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
const handleDialogSetData = (params: Record<string, any>) => {
  
  if (!params.fileUrl) params.fileUrl = [];
  if (params.fileUrl) params.fileUrl = JSON.parse(params.fileUrl);
  
};
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};

// 处理对话框保存数据
const handleDialogSaveData =(params: Record<string, any>)=>{
  // if(params.productContractPrice) params.productContractPrice = params.productContractPrice.toFixed(2)
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};


defineExpose({
  open: handleOpenDialog,
});
</script>

<style lang="less" scoped>
.text-only-cascader {
  :deep(.t-input__suffix-icon) {
    display: none;
  }
  
}
</style>
